﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using BLL;
namespace AGVControlSystemPro
{
    public partial class FrmEditRemoteIO : Form
    {
        private RemoteIOManager objRemoteIOManager = new RemoteIOManager();
        List<ExCall> listExCall = new List<ExCall>();
        public IPTypeOutput ipTypeOutput;
        public FrmEditRemoteIO()
        {
            InitializeComponent();
            //轨道类型下拉数据加载
            this.cboTrackName.DataSource = new BLL.TrackInfoManager().GetAllTrack();
            this.cboTrackName.DisplayMember = "TrackName";
            this.cboTrackName.ValueMember = "TrackId";
            this.cboTrackName.SelectedIndex = -1;
            //开始加载所有配置号的远程IO
            this.dgvRemoteIO.AutoGenerateColumns = false;
            listExCall = objRemoteIOManager.GetAllRemoteIO();
            this.dgvRemoteIO.DataSource = listExCall;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.txtIP.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレスを入力してください", "情報のヒント");
                else MessageBox.Show("请输入远程IO的IP地址", "信息提示");
                this.txtIP.Focus();
                return;
            }
            if (!Common.DataValidate.IsIPAddress(this.txtIP.Text.Trim()))
            {               
                this.txtIP.Focus();
                this.txtIP.SelectAll();
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレス入力フォーマットエラー", "情報のヒント");
                else MessageBox.Show("远程IO的IP地址输入格式不正确", "信息提示");
                return;
            }
            if (this.txtRemoteName.Text.Trim().Length == 0)
            {
                
                this.txtRemoteName.Focus();
                if (CommonMethods.LanguageId == 3) MessageBox.Show("デバイスの名前を入力してください", "情報のヒント");
                else MessageBox.Show("请输入设备名称", "信息提示");
                return;
            }
            if (this.cboPortName.SelectedIndex == -1)
            {

                if (CommonMethods.LanguageId == 3) MessageBox.Show("ポート番号をお選びください", "情報のヒント");
                else MessageBox.Show("请选择远程IO端口号", "信息提示");
                this.cboPortName.Focus();
                return;
            }
            if (this.cboTrackName.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("所属軌道を選択してください", "情報のヒント");
                else MessageBox.Show("请选择所属轨道", "信息提示");
                this.cboTrackName.Focus();
                return;
            }
            #endregion

            //数据封装
            ExCall objExCall = new ExCall()
            {
                IP = this.txtIP.Text.Trim(),
                RemoteName = this.txtRemoteName.Text.Trim(),
                Port = 16+this.cboPortName.SelectedIndex,
                PortName = this.cboPortName.Text.Trim(),
                TrackId = Convert.ToInt32(this.cboTrackName.SelectedValue),
                TrackName = this.cboTrackName.Text.Trim(),
                Station1 = Convert.ToInt32(this.nudStation1.Value),
                Station2 = Convert.ToInt32(this.nudStation2.Value),
                Station3 = Convert.ToInt32(this.nudStation3.Value)
            };
            //后台数据访问
            try
            {
                int result = objRemoteIOManager.AddRemoteIO(objExCall);
                if (result == 1)
                {
                    //加载数据
                    listExCall = objRemoteIOManager.GetAllRemoteIO();
                    this.dgvRemoteIO.DataSource = null;
                    this.dgvRemoteIO.DataSource = listExCall;
                    DialogResult dResult;
                    if (CommonMethods.LanguageId == 3) dResult = MessageBox.Show("追加の成功を追加します。", "情報のヒント", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    else dResult = MessageBox.Show("远程IO添加成功，是否继续添加？", "添加提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dResult == DialogResult.Cancel)
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvRemoteIO.CurrentRow == null || this.dgvRemoteIO.Rows.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("変更が必要なデータを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择需要修改的数据", "修改提示");
                return;
            } 
            #region 数据验证
            if (this.txtIP.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレスを入力してください", "情報のヒント");
                else MessageBox.Show("请输入远程IO的IP地址", "信息提示");
                this.txtIP.Focus();
                return;
            }
            if (!Common.DataValidate.IsIPAddress(this.txtIP.Text.Trim()))
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("IPアドレス入力フォーマットエラー", "情報のヒント");
                else MessageBox.Show("远程IO的IP地址输入格式不正确", "信息提示");
                this.txtIP.Focus();
                this.txtIP.SelectAll();
                return;
            }
            if (this.txtRemoteName.Text.Trim().Length == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("デバイスの名前を入力してください", "情報のヒント");
                else MessageBox.Show("请输入设备名称", "信息提示");
                this.txtRemoteName.Focus();
                return;
            }
            if (this.cboPortName.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("ポート番号をお選びください", "情報のヒント");
                else MessageBox.Show("请选择远程IO端口号", "信息提示");
                this.cboPortName.Focus();
                return;
            }
            if (this.cboTrackName.SelectedIndex == -1)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("所属軌道を選択してください", "情報のヒント");
                else MessageBox.Show("请选择所属轨道", "信息提示");
                this.cboTrackName.Focus();
                return;
            }
            #endregion

            //数据封装
            ExCall objExCall = new ExCall()
            {
                RemoteID =Convert.ToInt32( this.dgvRemoteIO.CurrentRow.Cells["RemoteID"].Value),
                IP = this.txtIP.Text.Trim(),
                RemoteName = this.txtRemoteName.Text.Trim(),
                Port = 16 + this.cboPortName.SelectedIndex,
                PortName = this.cboPortName.Text.Trim(),
                TrackId = Convert.ToInt32(this.cboTrackName.SelectedValue),
                TrackName = this.cboTrackName.Text.Trim(),
                Station1 = Convert.ToInt32(this.nudStation1.Value),
                Station2 = Convert.ToInt32(this.nudStation2.Value),
                Station3 = Convert.ToInt32(this.nudStation3.Value)
            };
            try
            {
                int i = objRemoteIOManager.UpdateRemoteIO(objExCall);
                if (i == 1)
                { 
                   //加载数据
                    listExCall = objRemoteIOManager.GetAllRemoteIO();
                    this.dgvRemoteIO.DataSource = null;
                    this.dgvRemoteIO.DataSource = listExCall;
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvRemoteIO.CurrentRow == null || this.dgvRemoteIO.Rows.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("削除しなければならないデータを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择需要删除的数据", "信息提示");
                return;
            } 
            int remoteID = Convert.ToInt32(this.dgvRemoteIO.CurrentRow.Cells["RemoteID"].Value);
            DialogResult result  ;
            if (CommonMethods.LanguageId == 3) result = MessageBox.Show("削除を確認", "情報のヒント", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            else result =MessageBox.Show("确认删除编号：" + remoteID.ToString() + "数据?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                try
                {
                    int i =objRemoteIOManager.DeleteRemoteIO(remoteID.ToString());
                    if (i == 1)
                    {
                        //加载数据
                        listExCall = objRemoteIOManager.GetAllRemoteIO();
                        this.dgvRemoteIO.DataSource = null;
                        this.dgvRemoteIO.DataSource = listExCall;
                    }
                }
                catch (Exception ex)
                {
                    
                    MessageBox.Show(ex.Message);
                }
                
            }
                
        }

        private void dgvRemoteIO_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(this.dgvRemoteIO.CurrentRow ==null || this.dgvRemoteIO.Rows.Count ==0) return;
            this.txtIP.Text = this.dgvRemoteIO.CurrentRow.Cells["IP"].Value.ToString();
            this.txtRemoteName.Text = this.dgvRemoteIO.CurrentRow.Cells["RemoteName"].Value.ToString();
            this.cboPortName.Text = this.dgvRemoteIO.CurrentRow.Cells["PortName"].Value.ToString();
            this.cboTrackName.Text = this.dgvRemoteIO.CurrentRow.Cells["TrackName"].Value.ToString();
            this.nudStation1.Value = Convert.ToInt32(this.dgvRemoteIO.CurrentRow.Cells["Station1"].Value);
            this.nudStation2.Value = Convert.ToInt32(this.dgvRemoteIO.CurrentRow.Cells["Station2"].Value);
            this.nudStation3.Value = Convert.ToInt32(this.dgvRemoteIO.CurrentRow.Cells["Station3"].Value);
        }

        private void dgvRemoteIO_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvRemoteIO,e);
        }

        private void FrmEditRemoteIO_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmEditRemoteIO = null;
            ipTypeOutput();
        }
    }
}
